﻿using UnityEngine;
using System.Collections;
using PBMessage;

public class UnitMoverController : MonoBehaviour
{
    /// <summary>
    /// 0 停止移动 1移动
    /// </summary>
    int mCurMoverState;
    Vector3 mDir = new Vector3();
    GameScene mGameSceme;
    int mMoveSpeed;

    long mStartMoverTime;
    Vector3 mPreMovePos;

    Vector3 mTargetPos;
    void Start()
    {
        mGameSceme = SGLib.Scene.SceneManager.Instance.Cur<GameScene>();
    }

    public void Init(int moveSpeed)
    {
        this.mMoveSpeed = moveSpeed;
    }

    public void OnClientMover(int state, Vector3 dir, bool isPCType, Vector3 targetPos)
    {
        CS_MoveUnit moveUnit = new CS_MoveUnit();
        moveUnit.State = state;
        moveUnit.MoveDirX = (int)(dir.x * 100);
        moveUnit.MoveDirY = (int)(dir.z * 100);
        moveUnit.Time = mGameSceme.ServerTime;
        Game.Inst.Net.NetClient.Send((int)PBMesasgeID.CS_MoveUnit, moveUnit);
        if (isPCType)
        {
            mTargetPos = targetPos;
        }
    }

    public void OnServerMover(SC_MoveUnit scMoverUnit)
    {
        
        if (scMoverUnit.State == 1 || scMoverUnit.State == -1)
        {
            if (mCurMoverState == 1)
            {
               
                setPosByCurTime(scMoverUnit.Time);
                Debug.Log("移动重置");
            }
            mMoveSpeed = scMoverUnit.Speed;
            mCurMoverState = 1;
            mPreMovePos = transform.position;
            mDir.Set(scMoverUnit.DX / 100f, 0, scMoverUnit.DY / 100f);
            transform.position.Set(scMoverUnit.PosX / 100f, 0, scMoverUnit.PosY / 100f);
            mStartMoverTime = scMoverUnit.Time;
            transform.LookAt(transform.position + mDir);
            mDir.Normalize();
           // Debug.Log("PreMovePos" + mPreMovePos);
        }
        else if(scMoverUnit.State == 0)
        {
            mCurMoverState = 0;
            setPosByCurTime(scMoverUnit.Time);
            //Debug.Log(transform.position.ToString("F6"));
        }
    }


    public void ServerUpdate(long dt)
    {
        //if (mCurMoverState == 1)
        //{
        //    setPosByCurTime(mGameSceme.ServerTime);
        //}
    }

    void setPosByCurTime(long curTime)
    {
        int moveTime = (int)(curTime - mStartMoverTime);
        int moveNumber = this.mMoveSpeed * moveTime;
        transform.position = mPreMovePos + ((this.mDir * moveNumber) / 1000f);
    }

    void Update()
    {

        //PC 停止移动
        if (this.mCurMoverState == 1)
        {
            if (Vector3.Distance(mTargetPos, transform.position) < 1f)
            {
                mCurMoverState = 0;
                CS_MoveUnit moveUnit = new CS_MoveUnit();
                moveUnit.State = 0;
                moveUnit.MoveDirX = 0;
                moveUnit.MoveDirY = 0;
                moveUnit.Time = mGameSceme.ServerTime;
                Game.Inst.Net.NetClient.Send((int)PBMesasgeID.CS_MoveUnit, moveUnit);
            }
            setPosByCurTime(mGameSceme.ServerTime);
        }
    }
}
